/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __X_MODULE_H__
#define __X_MODULE_H__

/* includes (standard library, system) ---------------------------------------*/
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_compiler.h>
#include <x_config.h>
/* includes (local) ----------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

/* module id used in errno, syscall and so on */
#define MID_CRITICAL 0x00
#define MID_TICK     0x01
#define MID_TASK     0x02
#define MID_MUTEX    0x03
#define MID_SEM      0x04
#define MID_EVENT    0x05
#define MID_TIMER    0x06
#define MID_OBJ      0x07
#define MID_MQ       0x0f
#define MID_HEAP     0x10
#define MID_MEM      0x11
#define MID_MEMPOOL  0x12
#define MID_MEMTRY   0x13
#define MID_DEFER    0x18
#define MID_ERRNO    0x19
#define MID_SYSCALL  0x1a
#define MID_KPRINTF  0x1b
#define MID_HAL      0x40
#define MID_MPU      0x41
#define MID_CUSTOMER 0xff

/* typedefs ------------------------------------------------------------------*/

typedef int (*init_pfn)(void);

/* macro ---------------------------------------------------------------------*/

/**
 * MODULE_INIT - add a module init routine
 * @group: the init group name
 * @init:  the init routine
 *
 * supported groups for now:
 *
 *     *) cpu
 *     *) soc
 *     *) prekernel
 *     *) kernel
 *     *) postkernel
 *     *) hal
 *     *) service
 *     *) postservice
 *     *) bus
 *     *) predriver
 *     *) driver
 *     *) postdriver
 *     *) board
 *     *) postboard
 *     *) user
 *
 * the init senquence is the same as listed, and user is inited in task context
 * by deferred job task
 */
#ifdef CONFIG_OS_PONY
#define MODULE_INIT(group, init)                                                                   \
    const init_pfn __init_##group##_##init __section__(init_##group) = init
#else   // for Linux
#define MODULE_INIT(group, init)                                                                   \
    static void __attribute__((constructor, used)) __init_##group##_##init(void)                   \
    {                                                                                              \
        init();                                                                                    \
    }
#endif

/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __X_MODULE_H__ */
